// b2c/ad/ad.js
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    
  },
  adhref() {
    const self = this;
    wx.navigateTo({
      url: '../map/map?longitudes=' + self.data.longitudes + '&latitudes=' + self.data.longitudes + '&text=' + self.data.hrefaddress
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;
    this.mapCtx = wx.createMapContext('myMap')
    // 获取定位，并把位置标示出来
    app.getLocationInfo(locationInfo => {
      that.zb(locationInfo, (res) => {
        const data = res.data;
        if (data.info == "OK" && data.status == "1") {

          app.globalData.hrefaddress = `${data.regeocode.addressComponent.township}${data.regeocode.addressComponent.streetNumber.street}${data.regeocode.addressComponent.streetNumber.number}`;
          app.globalData.province = data.regeocode.addressComponent.province;
          app.globalData.city = data.regeocode.addressComponent.city;
          app.globalData.district = data.regeocode.addressComponent.district;
          app.globalData.long = locationInfo.longitude;
          app.globalData.lat = locationInfo.latitude;
          that.setData({
            longitude: locationInfo.longitude
            , latitude: locationInfo.latitude
            , longitudes: locationInfo.longitude
            , latitudes: locationInfo.latitude
            , province: app.globalData.province
            , city: app.globalData.city
            , district: app.globalData.district
            , hrefaddress: `${data.regeocode.addressComponent.township}${data.regeocode.addressComponent.streetNumber.street}${data.regeocode.addressComponent.streetNumber.number}`
            , markers: [
              {
                id: 0
                , iconPath: '../../images/icon/fill.png'
                , longitude: locationInfo.longitude
                , latitude: locationInfo.latitude
                , width: 30
                , height: 30
              }
            ]
          })
        }

      })


    })

    //set the width and height
    // 动态设置map的宽和高
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          map_width: res.windowWidth
          , map_height: res.windowWidth
          , controls: [{
            id: 1,
            iconPath: '../../images/icon/fill1.png',
            position: {
              left: res.windowWidth / 2 - 16,
              top: 300/2 - 32,
              width: 30,
              height: 30
            },
            clickable: true
          }]
        })
      }
    })

  },
  getLngLat: function () {
    var that = this;
    this.mapCtx = wx.createMapContext("map4select");
    this.mapCtx.getCenterLocation({
      success: function (res) {
        that.zb(res, (data) => {
          const json = data.data;
          console.log(json)
          if (json.info == "OK" && json.status == "1"){
            
            that.setData({
              longitude: res.longitude
              , latitude: res.latitude
              , province: app.globalData.province
              , city: app.globalData.city
              , district: app.globalData.district
              , hrefaddress: `${json.regeocode.addressComponent.township}${json.regeocode.addressComponent.streetNumber.street}${json.regeocode.addressComponent.streetNumber.number}`
              , markers: [
                {
                  id: 0,
                  iconPath: "../../images/icon/fill.png",
                  longitude: res.longitude,
                  latitude: res.latitude,
                  width: 30,
                  height: 30
                }
              ]
            })
          }
          
        })
      }
    })
  }
  , regionchange(e) {
    this.data.falg = false;
    // 地图发生变化的时候，获取中间点，也就是用户选择的位置    
    if (e.type == 'end') {
      this.getLngLat()
    } else {
      return;
    }
  }
  ,
  markertap(e) {
    console.log(e)
  },
  /*收货坐标 */
  zb(res, callback) {
    const self = this;
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_baidud",
      data: {
        latitude: res.latitude,
        longitude: res.longitude
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: (res) => {
        callback && callback(res)
      },
      fail: (res) => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  editor(res){
    const self = this;
    app.globalData.long = self.data.longitude;
    app.globalData.lat = self.data.latitude;
    app.globalData.province = self.data.province;
    app.globalData.city = self.data.city;
    app.globalData.district = self.data.district;
    app.globalData.hrefaddress = self.data.hrefaddress;
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})